package com.easylinkin.linkappapi.building.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.building.entity.Floor;
import com.easylinkin.linkappapi.building.entity.vo.SimpleBatchFloorVo;
import com.easylinkin.linkappapi.building.service.FloorService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.location.vo.AppFloorFileVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * Floor表控制层
 *
 * @author CodeGenerator
 * @date 2022/09/21
 */
@Slf4j
@RestController
@RequestMapping("floor")
public class FloorController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private FloorService appFloorService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<Floor> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Floor> record = appFloorService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appFloorService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appFloor 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody Floor appFloor) {
        return RestBuilders.successBuilder().success((this.appFloorService.saveOne(appFloor))).build();
    }

    /**
     * 新增数据
     *
     * @param appFloors 实体对象list
     * @return 新增结果
     */
    @PostMapping("insertBatch")
    @ApiOperation("新增")
    public RestMessage insertBatch(@RequestBody List<Floor> appFloors) {
        return RestBuilders.successBuilder().success((this.appFloorService.insertBatch(appFloors))).build();
    }

    /**
     * 新增数据
     *
     * @param simpleBatchFloorVos 实体对象list
     * @return 新增结果
     */
    @PostMapping("simpleInsertBatch")
    @ApiOperation("新增")
    public RestMessage simpleInsertBatch(@RequestBody List<SimpleBatchFloorVo> simpleBatchFloorVos) {
        return RestBuilders.successBuilder().success((this.appFloorService.simpleInsertBatch(simpleBatchFloorVos))).build();
    }

    /**
     * 修改数据
     *
     * @param appFloor 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody Floor appFloor) {
        return RestBuilders.successBuilder().success(this.appFloorService.updateOne(appFloor)).build();
    }

    /**
     * 修改单条指定字段
     *
     * @param appFloor 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    @ApiOperation("修改单条指定字段")
    public RestMessage updateOnlyOne(@RequestBody Floor appFloor) {
        return RestBuilders.successBuilder().success(this.appFloorService.updateOnlyOne(appFloor)).build();
    }

    /**
     * 批量修改图纸
     *
     * @param appFloorFileVO
     * @return
     */
    @PostMapping("updateUrlBatch")
    @ApiOperation("批量修改图纸")
    public RestMessage updateUrlBatch(@RequestBody AppFloorFileVO appFloorFileVO) {
        return RestBuilders.successBuilder().success(this.appFloorService.updateBatch(appFloorFileVO)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Integer> idList) {
        return RestBuilders.successBuilder().success(this.appFloorService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody Floor appFloor, HttpServletRequest request, HttpServletResponse response) {
        appFloorService.export(appFloor, request, response);
    }
}

